import random
import time
from enum import Enum, auto
from typing import Union

from api.Command import Command
from api.DeviceOperator import initDeviceCommand
from api.InternalConfig import *
from minidevice import MiniDevice
from loguru import logger


class YaBiaoState(Enum):
    # 无任务
    NoTask = auto()
    # 运送中
    DeliveryRun = auto()
    # 劫持进入待验证
    RobbingBegin = auto()
    # 劫持进入已验证,并进入自动状态
    RobbingAuto = auto()
    # 劫持结束,取消自动状态
    RobbingEnd = auto()
    # 到目的地,进行交付
    DeliveryFinish = auto()
    # 已完成所有的运送
    AllDone = auto()


class YaBiao(object):
    # 指令
    command_: Command = None
    # 当前的状态
    state = YaBiaoState.NoTask
    # 当前的坐标
    coordinate = [0, 0]

    def __init__(self, command: Command):
        self.command_ = command

    def checkPurpose(self):
        """
        检查此次的目的地,如果没有则返回空字符串
        :return: 目的地,没有则为空字符串
        """
        return ""

    def flyToGate(self):
        """
        使用77到门口去
        """
        # 打开道具
        self.command_.propsOpen()
        # 等待
        time.sleep(random.uniform(0.5, 0.8))

        # 点击77,弹出地点选择
        # 获得范围内的坐标
        x = random.randint(biao77WidthBounds[0], biao77WidthBounds[1])
        y = random.randint(biao77HeightBounds[0], biao77HeightBounds[1])
        # 双击
        self.command_.click(x, y)
        self.command_.click(x, y)
        # 等待
        time.sleep(random.uniform(0.2, 0.4))

        # 选择目的地,需要点两次,第一次触发放大地图,第二次会使用
        # 获得范围内的坐标
        x = random.randint(biao77PointWidthBounds[0], biao77PointWidthBounds[1])
        y = random.randint(biao77PointHeightBounds[0], biao77PointHeightBounds[1])
        # 双击
        self.command_.click(x, y)
        self.command_.click(x, y)
        # 等待
        time.sleep(random.uniform(0.3, 0.5))

        # 关闭道具
        self.command_.propsClose()

    def run(self):
        if self.command_ is None:
            # 设备未初始化
            logger.error("Device not initialized")
            return
        # 执行逻辑
        match self.state:
            case YaBiaoState.NoTask:
                # 飞到门口
                self.flyToGate()
                # 进门
                # 获得范围内的坐标
                x = random.randint(biaoComeinWidthBounds[0], biaoComeinWidthBounds[1])
                y = random.randint(biaoComeinHeightBounds[0], biaoComeinHeightBounds[1])
                self.command_.click(x, y)
                # 移动到领取点
                # 领取
                # 查看是否有验证
                # 有验证则验证
                # 切换状态到跑路状态
                self.state = YaBiaoState.DeliveryRun
                pass
            case YaBiaoState.DeliveryRun:
                pass
            case YaBiaoState.RobbingBegin:
                pass
            case YaBiaoState.RobbingAuto:
                pass
            case YaBiaoState.RobbingEnd:
                pass
            case YaBiaoState.DeliveryFinish:
                pass
            case YaBiaoState.AllDone:
                pass


if __name__ == '__main__':
    # 初始化设备
    command = initDeviceCommand()
    # 初始化实例
    yb = YaBiao(command)
    yb.flyToGate()
